package com.xjh.him.fragment.profile;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.xjh.him.R;
import com.xjh.him.common.Constant;
import com.xjh.him.core.BaseFragment;
import com.xjh.him.databinding.FragmentUpdateSignatureBinding;
import com.xjh.him.dto.user.UserDTO;
import com.xjh.him.utils.MMKVUtils;
import com.xjh.him.utils.Utils;
import com.xjh.him.utils.XToastUtils;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xui.utils.KeyboardUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;
import com.xuexiang.xutil.net.JsonUtil;

import org.xjh.tool.Checks;

@Page
public class UpdateSignatureFragment extends BaseFragment<FragmentUpdateSignatureBinding> {
    private UserDTO userDTO;

    @NonNull
    @Override
    protected FragmentUpdateSignatureBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentUpdateSignatureBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        String json = (String) MMKVUtils.get(Constant.MMKV_KEY_USER_DTO, "");
        if (!Checks.isEmpty(json)) {
            userDTO = JSON.parseObject(json, UserDTO.class);
        }
        if (userDTO != null) {
            binding.etSignature.setText(userDTO.getSignature());
        }
    }

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle();
        titleBar.setTitle("更新签名")
            .addAction(new TitleBar.TextAction("保存") {
                @Override
                public void performAction(View view) {
                    if (binding.etSignature.getText().length() > binding.etSignature.getMaxCharacters()) {
                        binding.etSignature.setHelperText(getString(R.string.tip_signature_length));
                        return;
                    }

                    String apiUrl = Utils.getWebServerApiUrl(requireActivity(), getString(R.string.api_user_update_info));
                    UserDTO params = new UserDTO();
                    params.setId(userDTO.getId());
                    params.setSignature(binding.etSignature.getText().toString());
                    SimpleCallBack<Boolean> updateCB = new SimpleCallBack<Boolean>() {
                        @Override
                        public void onSuccess(Boolean response) throws Throwable {
                            XToastUtils.toast("更新成功");
                        }

                        @Override
                        public void onError(ApiException e) {
                            XToastUtils.toast(e.getDisplayMessage());
                        }
                    };
                    Utils.doPost(apiUrl, JsonUtil.toJson(params), updateCB);
                    KeyboardUtils.hideSoftInputClearFocus(binding.etSignature);
                    popToBack();
                }
            });
        return titleBar;
    }
}
